package file_cleaner

import (
	"file-cleaner/internal/logger"
	"syscall"
)

func getSysRLimit() (uint64, error) {
	var rLimit syscall.Rlimit
	err := syscall.Getrlimit(syscall.RLIMIT_NOFILE, &rLimit)
	if err != nil {
		return 0, err
	}

	return rLimit.Max, err
}

func optimisePoolCap(num int) int {
	// Check poolCap is 0, or not
	if num < 1 {
		num = 100
	}

	// Check poolCap <= limit
	limit, err := getSysRLimit()
	if err != nil {
		logger.Error(err)
		return 2
	}

	if num > int(limit) {
		num = int(limit)
	}

	num = num >> 2

	if num < 1 {
		num = 2
	}

	return num
}
